// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.common with an alias.
import Vue from 'vue';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import App from './app.vue';
import Vue2Filters from 'vue2-filters';
import { ToastPlugin } from 'bootstrap-vue';
import router from './router';
import * as config from './shared/config/config';
import * as bootstrapVueConfig from './shared/config/config-bootstrap-vue';
import JhiItemCountComponent from './shared/jhi-item-count.vue';
import JhiSortIndicatorComponent from './shared/sort/jhi-sort-indicator.vue';
import InfiniteLoading from 'vue-infinite-loading';
import ActivateService from './account/activate/activate.service';
import RegisterService from './account/register/register.service';
import UserManagementService from '@/admin/user-management/user-management.service';
import LoginService from './account/login.service';
import AccountService from './account/account.service';
import AlertService from './shared/alert/alert.service';

import '../content/scss/vendor.scss';

import UserOAuth2Service from '@/entities/user/user.oauth2.service';
/* tslint:disable */

import RoomUserService from '@/entities/room-user/room-user.service';
import MenuService from '@/entities/menu/menu.service';
import CityListService from '@/entities/city-list/city-list.service';
import RoomGoodsService from '@/entities/room-goods/room-goods.service';
import RoomTypeService from '@/entities/room-type/room-type.service';
import RoomBrokerService from '@/entities/room-broker/room-broker.service';
import RoomCommunityService from '@/entities/room-community/room-community.service';
import RoomSupportingFacilitiesService from '@/entities/room-supporting-facilities/room-supporting-facilities.service';
import RoomTransactionPropertyService from '@/entities/room-transaction-property/room-transaction-property.service';
import RoomBasicPropertyService from '@/entities/room-basic-property/room-basic-property.service';
import RoomListingFeatureService from '@/entities/room-listing-feature/room-listing-feature.service';
import RoomUserPermissionService from '@/entities/room-user-permission/room-user-permission.service';
import RoomFileService from '@/entities/room-file/room-file.service';
import RoomUserBrokerChatListService from '@/entities/room-user-broker-chat-list/room-user-broker-chat-list.service';
import RoomUserBrokerChantMessageService from '@/entities/room-user-broker-chant-message/room-user-broker-chant-message.service';
import RoomRentBasicService from '@/entities/room-rent-basic/room-rent-basic.service';
import RoomGoodSearchService from '@/entities/room-good-search/room-good-search.service';
import RoomSearchGoodService from '@/entities/room-search-good/room-search-good.service';
import RoomCommunitySearchService from '@/entities/room-community-search/room-community-search.service';
// jhipster-needle-add-entity-service-to-main-import - JHipster will import entities services here

/* tslint:enable */
Vue.config.productionTip = false;
config.initVueApp(Vue);
config.initFortAwesome(Vue);
bootstrapVueConfig.initBootstrapVue(Vue);
Vue.use(Vue2Filters);
Vue.use(ToastPlugin);
Vue.component('font-awesome-icon', FontAwesomeIcon);
Vue.component('jhi-item-count', JhiItemCountComponent);
Vue.component('jhi-sort-indicator', JhiSortIndicatorComponent);
Vue.component('infinite-loading', InfiniteLoading);
const store = config.initVueXStore(Vue);

const loginService = new LoginService();
const accountService = new AccountService(store, router);

router.beforeEach((to, from, next) => {
  if (!to.matched.length) {
    next('/not-found');
  }

  if (to.meta && to.meta.authorities && to.meta.authorities.length > 0) {
    accountService.hasAnyAuthorityAndCheckAuth(to.meta.authorities).then(value => {
      if (!value) {
        sessionStorage.setItem('requested-url', to.fullPath);
        next('/forbidden');
      } else {
        next();
      }
    });
  } else {
    // no authorities, so just proceed
    next();
  }
});

/* tslint:disable */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>',
  router,
  provide: {
    loginService: () => loginService,
    activateService: () => new ActivateService(),
    registerService: () => new RegisterService(),
    userService: () => new UserManagementService(),

    userOAuth2Service: () => new UserOAuth2Service(),
    roomUserService: () => new RoomUserService(),
    menuService: () => new MenuService(),
    cityListService: () => new CityListService(),
    roomGoodsService: () => new RoomGoodsService(),
    roomTypeService: () => new RoomTypeService(),
    roomBrokerService: () => new RoomBrokerService(),
    roomCommunityService: () => new RoomCommunityService(),
    roomSupportingFacilitiesService: () => new RoomSupportingFacilitiesService(),
    roomTransactionPropertyService: () => new RoomTransactionPropertyService(),
    roomBasicPropertyService: () => new RoomBasicPropertyService(),
    roomListingFeatureService: () => new RoomListingFeatureService(),
    roomUserPermissionService: () => new RoomUserPermissionService(),
    roomFileService: () => new RoomFileService(),
    roomUserBrokerChatListService: () => new RoomUserBrokerChatListService(),
    roomUserBrokerChantMessageService: () => new RoomUserBrokerChantMessageService(),
    roomRentBasicService: () => new RoomRentBasicService(),
    roomGoodSearchService: () => new RoomGoodSearchService(),
    roomSearchGoodService: () => new RoomSearchGoodService(),
    roomCommunitySearchService: () => new RoomCommunitySearchService(),
    // jhipster-needle-add-entity-service-to-main - JHipster will import entities services here
    accountService: () => accountService,
    alertService: () => new AlertService(),
  },
  store,
});
